package com.me.study.javaBasic.collection;

import com.google.common.collect.Sets;
import com.me.common.pojo.Student;
import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * List 测试及常用 API
 */
public class TestList {

	// 同一个 set 中名字唯一
	private static final Set<Student> set1 = new HashSet<>();
	private static final Set<Student> set2 = new HashSet<>();

	static {
		set1.add(new Student(1, "ljy"));
		set1.add(new Student(2, "Tom"));
		set1.add(new Student(3, "Lucy"));
		set1.add(new Student(4, "Jack"));
		set1.add(new Student(5, "LiLei"));

		set2.add(new Student(2, "Tom"));
		set2.add(new Student(3, "Lucy"));
		set2.add(new Student(4, "Jack"));
		set2.add(new Student(5, "LiLei"));
		set2.add(new Student(null, "Pitt"));
		set2.add(new Student(null, "Jackson"));
	}

	/** 交集 */
	@Test
	public void intersection() {
		Sets.SetView<Student> diff = Sets.intersection(set1, set2);
		System.out.println("交集：" + diff);
	}

	/** 差集 */
	@Test
	public void difference() {
		// https://blog.csdn.net/huanyinghanlang/article/details/78774652
		Sets.SetView<Student> diff = Sets.difference(set1, set2);
		System.out.println("set1 相较于 set2 的差集" + diff);
		System.out.println("set1 相较于 set2 的差集" + diff(set1, set2));

		diff = Sets.difference(set2, set1);
		System.out.println("set2 相较于 set1 的差集" + diff);
		System.out.println("set2 相较于 set1 的差集" + diff(set2, set1));
	}

	/** 并集 */
	@Test
	public void union() {
		Sets.SetView<Student> union = Sets.union(set1, set2);
		System.out.println("并集" + union);
	}

	/**
	 * 假设 Student 是数据库对象，newSet 删了一些，新增了一些
	 */
	private static Set<Student> diff(Set<Student> oldSet, Set<Student> newSet) {
		Set<Student> result = new HashSet<>();

		// cache 缓存了修改的对象，这部分一定在 oldSet 中
		Map<Integer, Student> cache = new HashMap<>();
		for (Student student : newSet) {
			if (null != student.getId()) {
				cache.put(student.getId(), student);
				// update(); // 更新操作
			} else {
				// insert(); // 插入
			}
		}
		for (Student student : oldSet) {
			// old存在，new 不存在，需要删除
			if (!cache.containsKey(student.getId())) {
				result.add(student);
				// remove(student);
			}
		}

		return result;
	}

	@Test
	public void test() {
		int size = 5;//总时隙数
		int num = 3;//生成路由段的数量
		int timeSlotNumPerLink = size/num;//每条段包含的时隙数
		System.out.println(timeSlotNumPerLink);
		System.out.println("----------------------");
	}

}
